package com.xiyou.luckdraw.common.utils;

import org.apache.commons.lang.time.DateFormatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @ClassName: DateUtils
 * @Description: 时间格式化工具类
 * @author: limingxing
 * @Date: 2019-10-17 16:43
 */
public class DateUtils {

    private static final SimpleDateFormat YYYY_MM_DD_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 时间戳转换成日期格式字符串
     *
     * @param seconds 精确到秒的字符串
     * @param format
     * @return
     */
    public static String timeStamp2Date(String seconds, String format) {
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return "";
        }
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds)));
    }

    /**
     * 时间字符串转时间戳
     * @param dateStr
     * @param format
     * @return
     * @throws Exception
     */
    public static Long dateStampStr(String dateStr, String format) throws Exception {

        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = sdf.parse(dateStr);
        return date.getTime();
    }

    /**
     * 获取上一天
     *
     * @param day
     * @auther Jiangshijian
     * @date 2019-02-28 16:40
     */
    public static Date getPrev(Date day) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(day);
        ca.add(Calendar.DATE, -1);
        return ca.getTime();
    }

    /**
     * 获取过去第几天的日期
     * @param past
     * @return
     */
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }

    /**
     * 日期格式转换，格式yyyymmdd
     *
     * @param date
     * @auther Jiangshijian
     * @date 2019-03-16 14:48
     */
    public static Date getDate(String date) {

        try {
            return YYYY_MM_DD_FORMAT.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void main(String[] args) throws Exception {
        long nowTime =System.currentTimeMillis();
        long todayStartTime = nowTime - (nowTime + TimeZone.getDefault().getRawOffset())% (1000*3600*24);
        String s = timeStamp2Date(String.valueOf(todayStartTime),null);
        System.out.println(s);

        System.out.println(DateFormatUtils.format(new Date(), "yyyy-MM-dd 00:00:00"));
        System.out.println(DateFormatUtils.format(new Date(), "yyyy-MM-dd 23:59:59"));
    }

}
